/*
 * @Author: fishroom
 * @Date: 2022-10-14 14:02:06
 * @LastEditTime: 2023-01-03 11:43:38
 * @FilePath: \koa\app\controller\Sys\SysRoleController.ts
 */
import { Context } from "koa";
import SysRoleMenuServer from "../../server/Sys/SysRoleMenuServer";
import SysRoleServer from "../../server/Sys/SysRoleServer";
import response from "../../util/response";

/*
 * @Author: fishroom
 * @Date: 2022-10-14 14:02:06
 * @LastEditTime: 2022-10-21 10:54:43
 * @FilePath: \koa\app\controller\Sys\SysRoleController.ts
 */
class SysRoleController {
  async getRoleList(ctx: Context) {
    try {
      let params = ctx.query;
      // 初始化页码
      let limit = 10,
        page = 1;
      if (params.limit) {
        limit = Number(params.limit);
        delete params.limit;
      }
      if (params.page) {
        page = Number(params.page);
        delete params.page;
      }
      // 去除空参数
      for (let key in params) {
        if (!params[key]) {
          delete params[key];
        }
      }
      // 去除时间
      delete params["t"];
      const { rows, count } = await SysRoleServer.getRoleList(limit, (page - 1) * limit, params);
      return response.CxtSuccess(ctx, { page: rows, count, limit, currPage: page });
    } catch (error) {
      return response.CxtError(ctx, "失败", { error });
    }
  }
  async addRole(ctx: Context) {
    try {
      let obj = ctx.request.body;
      console.log(obj);
      let msg = await SysRoleServer.addRole(obj);
      await SysRoleMenuServer.updateRoleMenuListById(Number(msg.role_id), obj.menu_idList);
      response.CxtSuccess(ctx, { msg });
    } catch (error) {
      response.CxtError(ctx, "失败", { error });
    }
  }
  async deleteRole(ctx: Context) {
    try {
      let user_ids = ctx.request.body;
      let msg = user_ids.forEach(async (user_id: number) => {
        await SysRoleServer.deleteRoleById(user_id);
      });

      response.CxtSuccess(ctx, { msg });
    } catch (error) {
      response.CxtError(ctx, "错误", { error });
    }
  }
  async updateRoleById(ctx: Context) {
    let obj = ctx.request.body;
    try {
      await SysRoleServer.updateRoleById(obj);
      await SysRoleMenuServer.updateRoleMenuListById(Number(obj.role_id), obj.menu_idList);
      response.CxtSuccess(ctx, { msg: "修改成功" });
    } catch (error) {
      response.CxtError(ctx, "失败", { error });
    }
  }
  async getRoleInfo(ctx: Context) {
    console.log("进入");
    
    try {
      let params = ctx.query;
      // 去除空参数
      for (let key in params) {
        if (!params[key]) {
          delete params[key];
        }
      }
      // 去除时间
      delete params["t"];
      console.log("开始",params);
      
      const { rows, count } = await SysRoleServer.getRoleList(1, 0, params);
      console.log("rows", rows);

      let objArr = await SysRoleMenuServer.getRoleMenuListById(rows[0].role_id);
      let menu_idList = objArr.map((item) => {
        return item.menu_id;
      });
      return response.CxtSuccess(ctx, { role: rows[0], menu_idList });
    } catch (error) {
      return response.CxtError(ctx, "失败", { error });
    }
  }
}
export default new SysRoleController();
